package com.qqtop10.manager.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qqtop10.manager.model.temp.Goods;
import com.qqtop10.manager.model.IntCategory;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.GoodsService;

@RequestMapping("/manager/goods/")
@Controller
public class GoodsController extends BaseController {
	@Autowired
	private GoodsService goodsService;

	/**
	 * 条件性获取商品信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getGoodsList.do")
	public void getOrderDetailedByUuid(HttpServletRequest request, HttpServletResponse response, String shopName,
			String goodsName, String type) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/javascript; charset=utf-8");
		Map<String, Object> map = new HashMap<>();
		StringBuffer where = new StringBuffer();
		// 必须是未删除的
		where.append("WHERE tg.isdeleted = 0 ");
		if (shopName != null && !shopName.equals("")) {
			where.append("AND ts.name like '%" + shopName + "%'");
		}
		if (goodsName != null && !goodsName.equals("")) {
			where.append("AND tg.name like '%" + goodsName + "%'");
		}
		// 普通
		if (type != null && type.equals("0")) {
			where.append("AND tg.recommend = 0 ");
		}
		// 已推荐
		else if (type != null && type.equals("1")) {
			where.append("AND tg.recommend = 1 ");
		}
		map.put("where", where);
		map.put("column", "tg.id,tg.uuid,tg.uuid,tg.name,tg.shop_uuid,tg.recommend,tg.recommend_end,ts.name tags");
		map.put("order", " ORDER BY tg.id desc ");
		map.put("from", "from tb_goods tg LEFT JOIN tb_shop ts ON tg.shop_uuid = ts.uuid");
		response.getWriter().print(super.page(map, goodsService, request));
	}

	@SuppressWarnings("deprecation")
	@RequestMapping("recommend.do")
	@ResponseBody
	public Result recommend(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String recommendEnd = request.getParameter("recommend_end");
		String name = request.getParameter("name");
		Goods input = new Goods();
		if (id == null || recommendEnd == null) {
			return err("有些必须的参数未指定。");
		}

		DateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sf.parse(recommendEnd);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		input.setId(Integer.parseInt(id));
		input.setRecommend_end(date);
		input.setName(name);
		if (input.getRecommend_end().getTime() <= System.currentTimeMillis()) {
			return err("结束时间不能小于当前时间。");
		}
		try {
			input.setRecommend(1);
			input.setUpdatetime(now());
			input.setMender(getLogindUuid());
			if (goodsService.recommend(input)) {
				return html("操作成功!", 0);
			}
			return err("操作失败!");
		} catch (Exception e) {
			return excep(e);
		}
	}

	/**
	 * 移除推荐
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("removeRecommend.do")
	@ResponseBody
	public Result removeRecommend(HttpServletRequest request, HttpServletResponse response) {
		Result result = null;
		String[] uuids = null;
		try {
			result = new Result(1, "移除推荐成功");
			uuids = request.getParameterValues("uuids[]");
			if (uuids == null) {
				uuids = request.getParameterValues("uuids");
			}
			int i = goodsService.removeRecommendByUuid(Arrays.asList(uuids));
			if (i != uuids.length) {
				result.setCode(99);
				result.setMsg("移除推荐失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据国家ID查询对应国家的商品
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping("rcmdGlobalGoods.do")
	@ResponseBody
	public Result recommendGlobalGoods(HttpServletRequest request, HttpServletResponse response, Integer id) {
		List<Goods> goodsList = null;
		try {
			goodsList = goodsService.selectGlobalShoppingGoodsByCountryId(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Result result = new Result();
		result.setCode(0);
		result.setData(goodsList);
		result.setMsg("获取数据列表成功");
		return result;
		// return ok("操作成功", goodsList);
	}
}
